/**Created by the LayaAirIDE*/
package jnBase.view
{
	import jnBase.Data;
	import jnBase.components.BaseBrush;
	import jnBase.components.CaptureRect;
	
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.maths.Point;
	import laya.resource.HTMLCanvas;
	import laya.ui.Box;
	import laya.ui.Image;
	import laya.utils.Browser;
	import laya.utils.Ease;
	import laya.utils.Handler;
	import laya.utils.Tween;
	
	import ui.tools.PPTUI;
	
	
	public class PPTPlayer extends PPTUI
	{
		private var bigImgPos:Point;
		public var imgDataSource:Array=[];
		
		public function PPTPlayer() 
		{
			super();
			size(1349,973);
		}
		
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			super.onAwake();
			if(Data.PracticeId){
				getPPT(Data.practiceInfo.ppt)
			}
			else{
				useLocal()
			}
			function getPPT(data:Array):void
			{
				// TODO Auto Generated method stub
				if(data.length>0){
					Data.PPTDataSource=data;
				}
				useLocal()
			}
			function useLocal(){
				imgDataSource=Data.PPTDataSource;
				init();
			}
		}
		
		private var bigimgSize:Point;
		private function init():void
		{
			/*btn_close.on(Event.CLICK,this,function ():void{
				removeSelf();
			});*/
			bigimgSize=new Point();
			bigimgSize.setTo(bigimg.width,bigimg.height);
			bigImgPos=new Point(bigimg.width/2,bigimg.height/2);
			trace("bigimg.x:"+bigimg.width/2,"bigimg.y:"+bigimg.height/2);
			btn_down.visible=true;
			brush=new BaseBrush(captureBg.width,captureBg.height);
			canva.addChild(brush);
			toolsBox();
			simgList.hScrollBarSkin="";
			simgList.scrollBar.elasticBackTime=500;
			simgList.scrollBar.elasticDistance=200;
			
			simgList.renderHandler=new Handler(this,renderHandler);
			simgList.mouseHandler=new Handler(this,clickHandler);
			simgList.selectHandler=new Handler(this,selectHandler);
//			getData();
			setSource(imgDataSource);
		}
		public function saveImg($data:*,$fileName:String):void{
			var save_link:*=Browser.document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
			save_link.href=$data;
			save_link.download=$fileName;
			var event:*=Browser.document.createEvent('MouseEvents');
			event.initMouseEvent('click',true,false,Browser.window,0,0,0,0,0,false,false,false,false,0,null);
			save_link.dispatchEvent(event);
		}
		/**
		 *增加菜单栏工具功能 
		 * 
		 */		
		private function toolsBox():void{
			bsize.max=10;
			bsize.min=1;
			bsize.changeHandler=new Handler(this,sizeHandler);
			bcolor.changeHandler=new Handler(this,onChangeColor);
			eraser.on(Event.CLICK,this,onEraser);
			btnClear.on(Event.CLICK,this,onClear);
			btn_down.on(Event.CLICK,this,onImgIcons);
			btn_up.on(Event.CLICK,this,onImgIcons);
			btnFull.on(Event.CLICK,this,onFull);
			btnBrush.on(Event.CLICK,this,onBrush);
			btnCircle.on(Event.CLICK,this,onCircle);
			btnLine.on(Event.CLICK,this,onLine);
			btnRect.on(Event.CLICK,this,onRect);
			btnShowTools.on(Event.CLICK,this,hideRightTools);
			btnCaptureScreen.on(Event.CLICK,this,onCapture);
		}
		
		private function sizeHandler($value:Number):void
		{
			// TODO Auto Generated method stub
			brush.lineWidth=$value;
		}
		
		private function onCapture():void
		{
			// TODO Auto Generated method stub
			/*onFull();
			Laya.timer.once(300,this,captureScreen);*/
			addCaptureRect();
		}
		
		private function addCaptureRect():void
		{
			// TODO Auto Generated method stub
			var cap:CaptureRect=new CaptureRect(captureBg.width,captureBg.height);
			this.addChild(cap);
			cap.pos(mP.x,mP.y);
			cap.on("captureRect",this,captureScreen);
		}
		
		private function captureScreen(rect:Sprite):void
		{
			// TODO Auto Generated method stub
			var htmlCanvas:HTMLCanvas=captureBg.drawToCanvas(rect.width,rect.height,-rect.x,-rect.y);
			var can:*=htmlCanvas._source;
			trace("截图："+rect.width,rect.height,rect.x,rect.y);
			saveImg(can.toDataURL("image/png"),"截图"+new Date().getDate()+""+Math.round((Math.random()*10000)));
		}
		private function onRect():void
		{
			// TODO Auto Generated method stub
			brush.isRect=true;
		}
		
		private function onLine():void
		{
			// TODO Auto Generated method stub
			brush.isLine=true;
		}
		
		private function onCircle():void
		{
			// TODO Auto Generated method stub
			brush.isCircle=true;
		}
		private function hideRightTools():void
		{
			// TODO Auto Generated method stub
			if(!isShowRight){
				showRight();
			}
			else{
				hideRight();
			}
		}
		/**
		 *隐藏菜单栏 
		 * 
		 */		
		private function hideRight():void
		{
			// TODO Auto Generated method stub
			Tween.to(rightTools,{x:this.width,alpha:0},300);
			btnShowTools.rotation=180;
			isShowRight=false;
		}
		/**
		 *显示菜单栏 
		 * 
		 */		
		private function showRight():void
		{
			// TODO Auto Generated method stub
			Tween.to(rightTools,{x:mP.width-rightTools.width,alpha:1},300);
			btnShowTools.rotation=0;
			isShowRight=true;
		}
		private var isShowRight:Boolean=true;
		/**
		 *隐藏菜单栏和缩略图栏
		 * 
		 */		
		private function onFull():void
		{
			// TODO Auto Generated method stub
				hideRight();
				hideImgicon();
		}
		/**
		 *显隐缩略图 
		 * 
		 */		
		private function onImgIcons():void
		{
			// TODO Auto Generated method stub
			if(imgIconsShow){
				hideImgicon();
			}else{
				showImgicon();
			}
		}
		/**
		 *全部清除 
		 * 
		 */		
		private function onClear():void
		{
			// TODO Auto Generated method stub
			brush.clear();
		}
		/**
		 *画笔模式 
		 * 
		 */		
		private function onBrush():void
		{
			// TODO Auto Generated method stub
			brush.isEraser=false;
			brush.isAnyDraw=true;
		}
		/**
		 *橡皮模式 
		 * 
		 */		
		private function onEraser():void
		{
			// TODO Auto Generated method stub
			brush.isEraser=true;
		}
		
		
		
		private function onChangeColor():void
		{
			// TODO Auto Generated method stub
			brush.lineColor=bcolor.selectedColor;
		}
		public function setSource($list:Array):void{
			trace("setSource:");
			imgDataSource=$list;
			simgList.dataSource=imgDataSource;
			simgList.selectedIndex=index;
		}
		/**
		 *显示缩略图 
		 * 
		 */		
		private function showImgicon():void
		{
			// TODO Auto Generated method stub
			imgIconsShow=true;
			btn_down.visible=true;
			btn_up.visible=false;
			Tween.to(listbg,{y:mP.height-listbg.height,alpha:1},300);
			Laya.timer.once(5000,this,hideImgicon);
			Tween.to(spdes,{y:mP.height-listbg.height-spdes.height},300,null);
			Tween.to(btn_up,{y:mP.height-listbg.height-btn_up.height},300,null);
			Tween.to(btn_down,{y:mP.height-listbg.height-btn_down.height},300,null);
		}
		/**隐藏缩略图*/
		private function hideImgicon():void
		{
			// TODO Auto Generated method stub
			btn_down.visible=false;
			btn_up.visible=true;
			imgIconsShow=false;
			Tween.to(listbg,{y:mP.height,alpha:0},300,null);
			Tween.to(spdes,{y:mP.height-spdes.height},300,null);
			Tween.to(btn_up,{y:mP.height-btn_up.height},300,null);
			Tween.to(btn_down,{y:mP.height-btn_down.height},300,null);
		}
		
		private var index:int=0;
		private var brush:BaseBrush;
		private var imgIconsShow:Boolean=true;
		private static var _ins:PPTPlayer;
		/**
		 *自动循环播放 
		 * 
		 */		
		private function loop():void
		{
			// TODO Auto Generated method stub
			index++;
			if(index>imgDataSource.length-1){
				index=0;
			}
			simgList.selectedIndex=index;
		}
		private var clickAble:Boolean=true;
		private function selectHandler():void
		{
			// TODO Auto Generated method stub
			swipeImg();
			des.text=simgList.selectedItem.title;
		}
		
		private function setAble():void
		{
			// TODO Auto Generated method stub
			clickAble=true;
			trace("clickAble:"+clickAble);
		}
		
		private function swipeImg():void
		{
			// TODO Auto Generated method stub
			var n:int=1+Math.round(Math.random()*6);
//			trace("n:"+n)
//			n=1;
			brush.clear();
//			Tween.clearAll(bigimg);
//			Tween.clearAll(img);
			var img:Image=new Image();
			img.anchorX=0.5;
			img.anchorY=0.5;
			img.skin=simgList.selectedItem.img;
			img.width=bigimg.width;
			img.height=bigimg.height;
			img.cacheAs="bitmap";
			var ms:Sprite;
			switch(n)
			{
				case 1://缩放渐变出现
				{
					Tween.to(bigimg,{scaleX:0.01,scaleY:0.01,alpha:0},300,null,Handler.create(this,destroyImg,[bigimg]));
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
//					img.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.scale(0.01,0.01);
					img.alpha=0;
					Tween.to(img,{scaleX:1,scaleY:1,alpha:1},800,Ease.bounceIn,null);
					bigimg=img;
					break;
				}
				case 2://旋转缩放渐变出现
				{
					Tween.to(bigimg,{scaleX:0.01,scaleY:0.01,alpha:0,rotation:360},300,null,Handler.create(this,destroyImg,[bigimg]));
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
//					img.graphics.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.scale(0.01,0.01);
					img.alpha=0;
					img.rotation=0;
					Tween.to(img,{scaleX:1,scaleY:1,alpha:1,rotation:360},800,Ease.circIn,null);
					bigimg=img;
					break;
				}
				case 3://左滑动
				{
					Tween.to(bigimg,{x:bigimg.x-bigimg.width,alpha:0},1000,Ease.elasticInOut,Handler.create(this,destroyImg,[bigimg]));
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
//					img.graphics.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.x=bigImgPos.x+bigimg.width;
					img.alpha=0;
					Tween.to(img,{x:bigImgPos.x,alpha:1},1000,Ease.elasticInOut,Handler.create(this,destroyImg,[bigimg]));
					bigimg=img;
					break;
				}
				case 4://右滑动
				{
					Tween.to(bigimg,{x:bigimg.x+bigimg.width,alpha:0},1000,Ease.elasticInOut,Handler.create(this,destroyImg,[bigimg]));
					tempimg.addChild(img);
					img.pos(bigImgPos.x-bigimg.width,bigImgPos.y);
//					img.graphics.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.alpha=0;
					Tween.to(img,{x:bigImgPos.x,alpha:1},1000,Ease.elasticInOut);
					bigimg=img;
					break;
				}
				case 5://渐变
				{
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
//					img.graphics.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.alpha=0;
					Tween.to(img,{alpha:1},800,Ease.elasticInOut,Handler.create(this,destroyImg,[bigimg]));
					bigimg=img;
					break ;
				}
				case 6://下方出现
				{
					Tween.to(bigimg,{alpha:0,y:bigimg.y-bigimg.height},1000,Ease.circInOut);
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y+bigimg.height);
//					img.graphics.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.alpha=0;
					Tween.to(img,{alpha:1,y:bigImgPos.y},1000,Ease.circInOut,Handler.create(this,destroyImg,[bigimg]));
					bigimg=img;
					break ;
				}
				case 7://上方出现
				{
					Tween.to(bigimg,{alpha:0,y:bigimg.y+bigimg.height},1000,Ease.circInOut);
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y-bigimg.height);
//					img.graphics.loadImage(simgList.selectedItem.img,0,0,bigimg.width,bigimg.height);
					img.skin=simgList.selectedItem.img;
					img.alpha=0;
					Tween.to(img,{alpha:1,y:bigImgPos.y},1000,Ease.circInOut,Handler.create(this,destroyImg,[bigimg]));
					bigimg=img;
					break ;
				}
				case 8://百叶窗
				{
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
					ms=new Sprite();
					baiyechuang("down",ms);
					img.cacheAs="bitmap";
					ms.cacheAs="bitmap";
					img.mask=ms;
					Laya.timer.frameLoop(1,this,function():void{
						img.repaint();
					});
					Laya.timer.once(1520,this,function():void{
						destroyImg(bigimg);
						bigimg=img;
					})
					break ;
				}
				case 9://百叶窗
				{
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
					ms=new Sprite();
					img.cacheAs="bitmap";
					ms.cacheAs="bitmap";
					img.mask=ms;
					baiyechuang("left",ms);
					Laya.timer.frameLoop(1,this,function():void{
						img.repaint();
					});
					Laya.timer.once(1520,this,function():void{
						destroyImg(bigimg);
						img.mask=null;
						ms.destroy();
						bigimg=img;
					})
					break ;
				}
				case 10://百叶窗
				{
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
					ms=new Sprite();
					baiyechuang("right",ms);
					ms.cacheAs="bitmap";
					img.cacheAs="bitmap";
					img.mask=ms;
					Laya.timer.frameLoop(1,this,function():void{
						img.repaint();
					});
					Laya.timer.once(1520,this,function():void{
						destroyImg(bigimg);
						bigimg=img;
					})
					break ;
				}
				case 11://百叶窗
				{
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
					ms=new Sprite();
					baiyechuang("up",ms);
					img.cacheAs="bitmap";
					ms.cacheAs="bitmap";
					img.mask=ms;
					Laya.timer.frameLoop(1,this,function():void{
						img.repaint();
					});
					Laya.timer.once(1520,this,function():void{
						destroyImg(bigimg);
						bigimg=img;
					})
					break ;
				}
			
				case 12://圆扩展
				{
					tempimg.addChild(img);
					img.pos(bigImgPos.x,bigImgPos.y);
					ms=new Sprite();
					ms.graphics.drawCircle(0,0,50,"#ffffff");
					img.cacheAs="bitmap";
					ms.cacheAs="bitmap";
					img.mask=ms;
					ms.pos(bigImgPos.x,bigImgPos.y);
					Laya.timer.frameLoop(1,this,function scaleCircle():void{
						img.repaint();
						ms.scaleX+=0.5;
						ms.scaleY+=0.5;	
						if(ms.scaleX>15){
							destroyImg(bigimg); 
							bigimg=img;
							img.mask=null;
							ms.destroy();
							Laya.timer.clearAll(this);
						}
					});
					break ;
				}
				default:
				{
					break;
				}
			}
		}
		
		private function baiyechuang(direction:String,$ms:Sprite):void
		{
			// TODO Auto Generated method stub
			for (var i:int = 0; i < 10; i++) 
			{
				var rect:Sprite=new Sprite();
				$ms.addChild(rect);
				switch(direction)
				{
					case "left":
					{
						rect.size(bigimg.width,bigimg.height/i);
						rect.graphics.drawRect(0,0,bigimg.width,80,"#ffffff");
						rect.pos(bigimg.width,80*i);
						Tween.to(rect,{x:0},800,Ease.circInOut,null,80*i);
						break;
					}
					case "right":
					{
						rect.graphics.drawRect(0,0,bigimg.width,80,"#ffffff");
						rect.pos(-bigimg.width,80*i);
						Tween.to(rect,{x:0},800,Ease.circInOut,null,80*i);
						break ;
					}
					case "up":
					{
						rect.graphics.drawRect(0,0,200,bigimg.height,"#ffffff");
						rect.pos(200*i,bigimg.height);
						Tween.to(rect,{y:0},800,Ease.circInOut,null,80*i);
						break ;
					}
					case "down":
					{
						rect.graphics.drawRect(0,0,200,bigimg.height,"#ffffff");
						rect.pos(200*i,-bigimg.height);
						Tween.to(rect,{y:0},800,Ease.circInOut,null,80*i);
						break ;
					}
					default:
					{
						break;
					}
				}
			}
			
		}
		
		private function destroyImg($old:Image):void
		{
			// TODO Auto Generated method stub
			trace("^^^^^^^^^^^^^^^^^^^destroyImg:");
			$old.graphics.clear();
			$old.destroy();
			$old.removeSelf();
		}
		private function clickHandler(e:Event,index:int):void
		{
			// TODO Auto Generated method stub
			if(e.type==Event.CLICK){//鼠标点击事件
				trace("select:"+clickAble+simgList.selectedItem);
				simgList.selectedIndex=index;
			}
		}
		
		private function renderHandler(cell:Box,index:int):void
		{
			// TODO Auto Generated method stub
			var simg:Image=cell.getChildByName("img") as Image;
//			var des:Label=cell.getChildByName("label") as Label;
			simg.skin=imgDataSource[index].img;
//			simg.loadImage(imgDataSource[index].img);
//			des.text="";
		}
		
		public function dispose():void
		{
			// TODO Auto Generated method stub
			this.destroy();
			Tween.clearAll(bigimg);
			Laya.timer.clear(this,loop);
		}
		public static function getInstance():PPTPlayer
		{
			_ins||=new PPTPlayer();
			return _ins;
		}
	}
	
}